//
//  ZHRentMessageViewController.m
//  sc-cyan-ios
//
//  Created by lj on 2017/12/26.
//  Copyright © 2017年 greenbuilding. All rights reserved.
//

#import "ZHRentMessageViewController.h"
#import "ZHProductPKViewController.h"
#import "ZHProductScreeningViewController.h"

#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件



#import "SWSearchNaviView.h"
#import "SWSearchListCollectionViewCell.h"
#import "ZHRentMessageCell.h"
#import "ZHCustomPaopaotView.h"
#import "ZHPointAnnotation.h"

static NSString *const cellId = @"RentMessage";

typedef enum : NSUInteger {
    RentHotTag = 10,//最热
    RentPKTag,//pk
    RentMapTag,//地图
    RentFiltrateHot,//筛选
} RentBtnTag;

@interface ZHRentMessageViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,BMKMapViewDelegate,BMKLocationServiceDelegate>
{
    NSInteger lastSelTag;
}
//顶部导航搜索框
@property (nonatomic, strong) SWSearchNaviView *searchNaviView;
//顶部父视图
@property (weak, nonatomic) IBOutlet UIView *topView;
//底部父视图
@property (weak, nonatomic) IBOutlet UIView *bottomView;
//列表
@property (nonatomic,strong) UICollectionView *collectionView;
//地图
@property (nonatomic,strong) BMKMapView* mapView;
//定位服务
@property (nonatomic,strong) BMKLocationService *locService;

@end

@implementation ZHRentMessageViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar addSubview:self.searchNaviView];
    
    [self.mapView viewWillAppear];
    self.mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [self.searchNaviView removeFromSuperview];
    
    [self.mapView viewWillDisappear];
    self.mapView.delegate = nil; // 不用时，置nil
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //默认选中最新
    UIButton *btn = [self.topView viewWithTag:10];
    lastSelTag = btn.tag;
    [self tagAction:btn];
    
    [self.collectionView isDisplayedInScreen] ?[self.collectionView removeFromSuperview] : [self.bottomView addSubview:self.collectionView] ;
    
}

- (UICollectionView *)collectionView{
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing = 10;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, Main_Screen_Height-self.topView.height - self.navigationController.navigationBar.height-[[UIApplication sharedApplication] statusBarFrame].size.height) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
        [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([ZHRentMessageCell class]) bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:cellId];
        
    }
    return _collectionView;
}

- (BMKMapView *)mapView{
    if (!_mapView) {
        _mapView = [[BMKMapView alloc] init];
        _mapView.x = 0;
        _mapView.y = 5;
        _mapView.width = Main_Screen_Width;
        _mapView.height = Main_Screen_Height-self.topView.height - self.navigationController.navigationBar.height-[[UIApplication sharedApplication] statusBarFrame].size.height - _mapView.y;
        _mapView.showsUserLocation = YES;//显示定位地图层
        _mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态为跟随模式
        _mapView.delegate = self;
        _mapView.isSelectedAnnotationViewFront = YES;
        
        _locService = [[BMKLocationService alloc] init];
        _locService.delegate = self;
        [_locService startUserLocationService];
        
        
        NSMutableArray *arr = [NSMutableArray array];
        for (int i = 0; i < 20; i++) {
            //大头针数据
            ZHPointAnnotation* annotation1 = [[ZHPointAnnotation alloc]init];
            annotation1.coordinate = CLLocationCoordinate2DMake(26.647540 + i * 0.01, 106.626183 + i*0.1);
            //后期这里改为模型字段就好了。
            annotation1.test = [NSString stringWithFormat:@"这是数据==%d",i];
            [arr addObject: annotation1];
        }
       
        [_mapView addAnnotations:arr];

    }
    return _mapView;
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ZHRentMessageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    
    return cell;
}

#pragma mark -- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((Main_Screen_Width - 30) / 2, (Main_Screen_Width - 30) / 2 + 60);
}


#pragma mark <BMKLocationServiceDelegate>

//实现相关delegate 处理位置信息更新
//处理方向变更信息
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
//    NSLog(@"heading is %@",userLocation.heading);
}
//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    NSLog(@"当前位置 %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    CLLocationCoordinate2D coor = CLLocationCoordinate2DMake(userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    self.mapView.centerCoordinate = coor;
}

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]])
    {
        static NSString *reuseIndetifier = @"annotationReuseIndetifier";
        BMKAnnotationView *annotationView = (BMKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[BMKAnnotationView alloc] initWithAnnotation:annotation
                                                           reuseIdentifier:reuseIndetifier];
        }
        
        annotationView.canShowCallout= YES;      //设置气泡可以弹出，默认为NO
        
        annotationView.image = [UIImage imageNamed:@"地图定位icon2.png"];//默认情况下
        
        CGFloat prace = 10;
        
        ZHPointAnnotation *point = (ZHPointAnnotation *)annotation;
        
        NSString *text = point.test;
        
        
        ZHCustomPaopaotView *customView = [ZHCustomPaopaotView initWithXib];
        customView.text = text;
        customView.width = 180;
        CGFloat h = [text sizeWithFont:SYSTEMFONT(12) andMaxSize:CGSizeMake(customView.width - prace * 2, MAXFLOAT)].height;
        customView.height = h + 15 * 4 + prace * 2;
        
        BMKActionPaopaoView *paopao = [[BMKActionPaopaoView alloc] initWithCustomView:customView];;
        annotationView.calloutOffset = CGPointMake(customView.width * 0.5 + prace, customView.height);
        annotationView.paopaoView = paopao;
        
        return annotationView;
    }
    return nil;
}

//选中大头针
- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view{
    
    view.image = [UIImage imageNamed:@"地图定位icon1.png"];
}

//取消中大头针
- (void)mapView:(BMKMapView *)mapView didDeselectAnnotationView:(BMKAnnotationView *)view{
    view.image = [UIImage imageNamed:@"地图定位icon2.png"];
}

/** 导航搜索框 */
- (SWSearchNaviView *)searchNaviView{
    if (!_searchNaviView) {
        _searchNaviView = [[SWSearchNaviView alloc] initWithFrame:self.navigationController.navigationBar.bounds];
        MJWeakSelf
        _searchNaviView.popViewBack = ^{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        };
        
        _searchNaviView.searchText = ^(NSString *text) {
            //            搜索新数据
            
            
//            [weakSelf.collectionView reloadData];
        };
        
    }
    return _searchNaviView;
}

/** 顶部标签按钮触发事件 */
- (IBAction)tagAction:(UIButton *)sender {
    
    if (sender.tag == RentHotTag || sender.tag == RentMapTag) {//点击最热和地图
        sender.selected = YES;
        if (sender.tag != lastSelTag) {
            UIButton *lastBtn = [self.topView viewWithTag:lastSelTag];
            lastBtn.selected = NO;
            lastSelTag = sender.tag;
            
            [self.collectionView isDisplayedInScreen] ?[self.collectionView removeFromSuperview] : [self.bottomView addSubview:self.collectionView] ;
            [self.mapView isDisplayedInScreen] ? [self.mapView removeFromSuperview] : [self.bottomView addSubview:self.mapView];
            
        }
    }else{
        switch (sender.tag) {
            case RentPKTag://pk页面
            {
                ZHProductPKViewController *pkVC = [[ZHProductPKViewController alloc] init];
                pkVC.view.backgroundColor = [UIColor whiteColor];
                pkVC.navigationItem.title = @"产品对比";
                
                [self.navigationController showViewController:pkVC sender:nil];
            }
                break;
            case RentFiltrateHot:
            {
                ZHProductScreeningViewController *screeningVC = [[ZHProductScreeningViewController alloc] init];
                screeningVC.view.backgroundColor = [UIColor whiteColor];
                screeningVC.navigationItem.title = @"筛选";
                
                [self.navigationController showViewController:screeningVC sender:nil];
                
            }
                break;
                
            default:
                break;
        }
    }
    
}

@end
